




//
//  homecollectionview.swift
//  mywei
//
//  Created by myd on 15/12/21.
//  Copyright © 2015年 myd1. All rights reserved.
//

import UIKit
import SDWebImage
class homecollectionview: UICollectionView {

    var message : homeweimessage?{
   didSet
   {
    self.reloadData()
        }
    }
    func calcviewsize() -> CGSize
    {
       
        let itemsize = CGSizeMake(90, 90)
        let margin = CGFloat(10)
  layout.itemSize = itemsize
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing=0
        let con = message?.pic_urlss?.count ?? 0
    
   if con == 1
   {
    var size = CGSizeMake(150, 120)
    let url = message?.pic_urlss![0]
   if let ima = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(url?.absoluteString)
   {
   size = ima.size
    if size.width<40{
    size.width=40
    }else
    {
   if size.width > UIScreen.mainScreen().bounds.size.width
   {
    size.width = UIScreen.mainScreen().bounds.size.width*0.5
        }
    }
    }
    layout.itemSize = size
    return size        }
        if con == 0{
       return CGSizeZero
        }
        
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing=margin
        if con == 4
    {
        let value = 2 * itemsize.width + margin
    return CGSizeMake(value, value)
        }
        let column = 3
        let width = CGFloat(column) * itemsize.width + (CGFloat(column)-1) * margin
        let row = (con + column - 1) / column
    let height = CGFloat(row) * itemsize.width + (CGFloat(row) - 1) * margin
        return CGSizeMake(width , height)
    
    }
    
    
    
    
    
   private var layout = UICollectionViewFlowLayout()
   init() {
        super.init(frame: CGRectZero, collectionViewLayout: layout)
    self.dataSource = self
    self.backgroundColor=UIColor.clearColor()
    self.registerClass(homemessage.self, forCellWithReuseIdentifier:"homecell")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


class  homemessage : UICollectionViewCell {
    var url : NSURL?  {
        didSet{
       image.sd_setImageWithURL(url)
        }}
    lazy var image : UIImageView =
        {
   let ima = UIImageView(image: nil)
            ima.contentMode = UIViewContentMode.ScaleToFill
            return ima
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func prepareUI()
    {
   addSubview(image)
   image.ff_Fill(contentView)
    }
}

extension homecollectionview : UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return message?.pic_urlss?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("homecell", forIndexPath: indexPath) as! homemessage
        cell.url = message?.pic_urlss![indexPath.item]
        return cell
    }
}